﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CDB.LinqModel.Data;
using CDB.LinqData;
using CDB.LinqModel;

public partial class addProperty : System.Web.UI.Page
{
    #region Variable Declaration
    IDataRepository _dataObject = new DataRepository();
    Property property;
    PropertyImage pImage;
    List<PropertyImage> pImages = new List<PropertyImage>();
    User user;
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["User"] == null)
            Response.Redirect("default.aspx");
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        User nUser = new User(_dataObject);
        nUser.UserName = Session["User"].ToString();
        nUser.GetUserByUserName();

        if (nUser.Id != 0)
        {
            property = new Property(_dataObject);
            property.Name = txtPropertyName.Text;
            property.AreaSqMt= txtArea.Text;
            property.Address1 = txtAddress1.Text;
            property.Address2 = txtAddress2.Text;
            property.City = txtCity.Text;
            //property.Price = Convert.ToDecimal(txtPrice.Text);
            //property.Luxury = txtLuxury.Text;
            //property.Transaction= txtTransaction.Text;
            property.Possession= txtPossession.Text;
            //property.Ownership = txtOwnership.Text;
            property.YearBuilt = txtPropertyAge.Text;
            property.Description = txtDescription.Text;
            property.Features = txtFeatures.Text;
            property.UserId = nUser.Id;
            property.DateAdded = System.DateTime.Now;
            property.Report = txtReport.Text;
            property.Issue = txtIssues.Text;

            property.Create();

            // Get the HttpFileCollection and save it to the respective folder.
            string path = Server.MapPath("PropertyImages") + "\\" + property.Id.ToString();

            if (!System.IO.Directory.Exists(path))
                System.IO.Directory.CreateDirectory(path);
            
            HttpFileCollection hfc = Request.Files;
            if (hfc.Count > 0)
            {
                for (int i = 0; i < hfc.Count; i++)
                {
                    HttpPostedFile hpf = hfc[i];
                    if (hpf.ContentLength > 0)
                    {
                        hpf.SaveAs(path + "\\" + System.IO.Path.GetFileName(hpf.FileName));
                        pImage = new PropertyImage();
                        pImage.PropertyId = property.Id;
                        pImage.Name = System.IO.Path.GetFileName(hpf.FileName);
                        pImages.Add(pImage);
                    }
                }
            }

            pImage = new PropertyImage(_dataObject);
            pImage.CreateImages(pImages);

            Session["myAccount"] = "Property added successfully.";
            Response.Redirect("addProperty.aspx");
        }
    }
}